package com.dkt.blogboot.controller;

import com.dkt.blogboot.entity.User;
import com.dkt.blogboot.req.UserInsertReq;
import com.dkt.blogboot.req.UserLoginReq;
import com.dkt.blogboot.resp.CommonResp;
import com.dkt.blogboot.resp.TagQueryResp;
import com.dkt.blogboot.resp.UserLoginResp;
import com.dkt.blogboot.service.UserService;
import com.dkt.blogboot.util.blogUserEs.ESOperation;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author 窦康泰
 * @Date 2020-08-01 13:17
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    ESOperation esOperation;
    @PostMapping("/reg")
    public CommonResp reg(@Valid @RequestBody UserInsertReq req) {
        req.setPassword(DigestUtils.md5DigestAsHex(req.getPassword().getBytes(StandardCharsets.UTF_8)));
        CommonResp<Object> resp = new CommonResp<>();
        userService.reg(req);
        this.afterUpdate();
        return resp;
    }

    @PostMapping("/login")
    public CommonResp login(@Valid @RequestBody UserLoginReq req) {
        req.setPassword(DigestUtils.md5DigestAsHex(req.getPassword().getBytes(StandardCharsets.UTF_8)));
        CommonResp resp = userService.login(req);
        UserLoginResp userLoginResp = (UserLoginResp) resp.getContent();
        redisTemplate.opsForValue().set(userLoginResp.getToken(), userLoginResp, 3600 * 24, TimeUnit.SECONDS);
        return resp;
    }

    @GetMapping("/logout/{token}")
    public CommonResp logout(@PathVariable("token") String token) {
        CommonResp resp = new CommonResp<>();
        redisTemplate.delete(token);
        return resp;
    }

    @GetMapping("/all")
    public CommonResp allUser() throws IOException {
        List<User> userList = esOperation.QueryFromES("user_reg_es", null, 0, 20);
        //List<User> userList = userService.selectAll();
        CommonResp<List<User>> resp = new CommonResp<>();
        resp.setContent(userList);
        return resp;
    }

    @GetMapping("/delete/{id}")
    public String deleteUser(@PathVariable("id") Integer id) {
        this.afterDelete(id);
        return userService.delete(id);
    }

    @PutMapping("/update")
    public String UpdateUser(@RequestBody User user) {
        this.afterUpdate();
        this.afterUpdate();
        return userService.update(user);
    }

    public void afterUpdate(){
        String exchangeName = "blog.direct";
        String routingKey="blog.update";
        rabbitTemplate.convertAndSend(exchangeName,routingKey,"updated");
        System.out.println("增改工单的消息发送完成");
    }

    public void afterDelete(int id){
        String exchangeName = "blog.direct";
        Integer message = id;
        String routingKey="blog.delete";
        rabbitTemplate.convertAndSend(exchangeName,routingKey,message);
        System.out.println("已删除ID为"+id+"的工单，同步消息发送完成！");
    }
}
